package com.bysj.agent.config;

import cn.hutool.core.util.StrUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Slf4j
@Component
public class TokenInterceptor implements RequestInterceptor {

    //    @Value("sa-token.token-name")
    private String tokenName = "token";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String tokenValue = requestAttributes.getRequest().getHeader(tokenName);
        if(StrUtil.isNotEmpty(tokenValue)){
            requestTemplate.header(tokenName,tokenValue);
            log.warn("token转发 - {}" , tokenValue);
        }else{
            log.warn("token转发 - 请求头中并没有Token");
        }

    }
}